package com.webserver_v5.controller;

import com.webserver_v5.annotation.Controller;
import com.webserver_v5.annotation.RequestMapping;
import com.webserver_v5.entity.User;
import com.webserver_v5.http.HttpServletRequest;
import com.webserver_v5.http.HttpServletResponse;
import com.webserver_v5.util.DBUtil;

import java.io.*;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class UserController {

    private static File userDir;

    static {
        userDir = new File("./users");
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理注册...");
        String username = request.getParameters("username");
        String password = request.getParameters("password");
        String nickname = request.getParameters("nickname");
        String ageStr = request.getParameters("age");
        //必要验证
        if(username==null||username.isEmpty()||
                password==null||password.isEmpty()||
                nickname==null||nickname.isEmpty()||
                ageStr==null||ageStr.isEmpty()||!ageStr.matches("[0-9]+")
        ){
            //信息输入有误提示页面
            response.sendRedirect("reg_info_error.html");
            return;
        }

        System.out.println(username+","+password+","+nickname+","+ageStr);

        int age = Integer.parseInt(ageStr);

//        User user = new User(username,password,nickname,age);
//        File file = new File(userDir,username+".obj");
//        if (file.exists()){
//            response.sendRedirect("have_user.html");
//            return;
//        }
//        try (
//                FileOutputStream fos = new FileOutputStream(file);
//             ObjectOutputStream oos = new ObjectOutputStream(fos);
//                ){
//            oos.writeObject(user);
//            response.sendRedirect("reg_success.html");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try
                (Connection connection = DBUtil.getConnection()) {
            String sql = "select 1 from userinfo where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs  = ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("have_user.html");
            }else {
                sql = "insert into userinfo(username,password,nickname,age)values (?,?,?,?)";
                ps = connection.prepareStatement(sql);
                ps.setString(1,username);
                ps.setString(2,password);
                ps.setString(3,nickname);
                ps.setInt(4,age);
                int n = ps.executeUpdate();
                if (n>0){
                    response.sendRedirect("reg_success.html");
                }else {
                    response.sendRedirect("reg_fail.html");
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理登录!!!");
        String username = request.getParameters("username");
        String password = request.getParameters("password");
        System.out.println(username+","+password);
        //必要的验证
        if(username==null||username.trim().isEmpty()||
                password==null||password.trim().isEmpty()){
            response.sendRedirect("login_info_error.html");
            return;
        }

        try (
                Connection connection = DBUtil.getConnection();
                ){
           String sql = "select id ,username,password,nickname,age from userinfo where username=? and password=?";
           PreparedStatement ps = connection.prepareStatement(sql);
           ps.setString(1,username);
           ps.setString(2,password);
           ResultSet rs = ps.executeQuery();
           if (rs.next()){
               response.sendRedirect("login_success.html");
           }else {
               response.sendRedirect("login_fail.html");

           }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
//        File file = new File(userDir,username+".obj");
//        if(file.exists()){//用户名是否存在(是否为一个注册用户)
//            try (
//
//                    FileInputStream fis = new FileInputStream(file);
//                    ObjectInputStream ois = new ObjectInputStream(fis);
//            ){
//                User user = (User)ois.readObject();//读取回来的是注册用户信息
//                //比较登录的密码和该注册用户的密码是否一致
//                if(user.getPassword().equals(password)){
//                    //登录成功
//                    response.sendRedirect("/login_success.html");
//                    return;
//                }
//            } catch (IOException | ClassNotFoundException e) {
//                e.printStackTrace();
//            }
        }

        //登录失败
    //    response.sendRedirect("/login_fail.html");
   // }

}
